export const HELM_TEMPLATE_LANGUAGE_CONF = {
  comments: {
    lineComment: '#',
    blockComment: ['{{- /*\n', '\n*/ -}}'],
  },
  brackets: [
    ['{{', '}}'],
    ['{{-', '-}}'],
    ['{', '}'],
    ['[', ']'],
    ['(', ')'],
  ],
  autoClosingPairs: [
    { open: '{{-', close: '-' },
    // ["\"", "\""],
    { open: '{', close: '}' },
    { open: '[', close: ']' },
    { open: '(', close: ')' },
    { open: '"', close: '"' },
    { open: "'", close: "'" },
  ],
  surroundingPairs: [
    { open: '{{', close: '}}' },
    { open: '{{-', close: '-}}' },
    { open: '{', close: '}' },
    { open: '[', close: ']' },
    { open: '(', close: ')' },
    { open: '"', close: '"' },
    { open: "'", close: "'" },
  ],
  folding: {
    offSide: true,
  },
};

export const HELM_TEMPLATE_LANGUAGE = {
  // brackets: [
  //   { token: 'delimiter.bracket', open: '{', close: '}' },
  //   { token: 'delimiter.tplBracket', open: '{{', close: '}}' },
  //   { token: 'delimiter.square', open: '[', close: ']' },
  // ],

  keywords: [
    'true',
    'True',
    'TRUE',
    'false',
    'False',
    'FALSE',
    'null',
    'Null',
    'Null',
    '~',
    '.Values',
    'AND',
    'and',
    'OR',
    'or',
    'NOT',
    'not',
  ],
  numberInteger: /(?:0|[+-]?[0-9]+)/,
  numberFloat: /(?:0|[+-]?[0-9]+)(?:\.[0-9]+)?(?:e[-+][1-9][0-9]*)?/,
  numberOctal: /0o[0-7]+/,
  numberHex: /0x[0-9a-fA-F]+/,
  numberInfinity: /[+-]?\.(?:inf|Inf|INF)/,
  numberNaN: /\.(?:nan|Nan|NAN)/,
  numberDate: /\d{4}-\d\d-\d\d([Tt ]\d\d:\d\d:\d\d(\.\d+)?(( ?[+-]\d\d?(:\d\d)?)|Z)?)?/,

  escapes: /\\(?:[btnfr\\"']|[0-7][0-7]?|[0-3][0-7]{2})/,
  // ignoreCase: true,
  tokenizer: {
    root: [
      [/\bnot\b/g, { token: 'keyword' }],
      [/\bor\b/g, { token: 'keyword' }],
      [/\band\b/g, { token: 'keyword' }],
      [/\brepo\b:/g, { token: 'string' }],
      [/\bfile\b:/g, { token: 'string' }],
      [/\blang\b:/g, { token: 'string' }],
      [/\bext\b:/g, { token: 'string' }],
      [/\bmine\b:/g, { token: 'string' }],
      [/\bauthor\b:/g, { token: 'string' }],
      [/\breviewer\b:/g, { token: 'string' }],
      [/\bmrstate\b:/g, { token: 'string' }],
      [/\bvisibility\b:/g, { token: 'string' }],
    ],
    // Flow Collection: Flow Mapping
    object: [
      { include: '@whitespace' },
      { include: '@comment' },

      // Flow Mapping termination
      [/\}/, '@brackets', '@pop'],

      // Flow Mapping delimiter
      [/,/, 'delimiter.comma'],

      // Flow Mapping Key:Value delimiter
      [/:(?= )/, 'operators'],

      // Flow Mapping Key:Value key
      [/(?:".*?"|'.*?'|[^,{[]+?)(?=: )/, 'type'],

      // Start Flow Style
      { include: '@flowCollections' },
      { include: '@flowScalars' },

      // Scalar Data types
      { include: '@tagHandle' },
      { include: '@anchor' },
      { include: '@flowNumber' },

      // Other value (keyword or string)
      [
        /[^},]+/,
        {
          cases: {
            // '@keywords': 'keyword',
            '@default': 'string',
          },
        },
      ],
    ],

    // Flow Collection: Flow Sequence
    array: [
      { include: '@whitespace' },
      { include: '@comment' },

      // Flow Sequence termination
      [/\]/, '@brackets', '@pop'],

      // Flow Sequence delimiter
      [/,/, 'delimiter.comma'],

      // Start Flow Style
      { include: '@flowCollections' },
      { include: '@flowScalars' },

      // Scalar Data types
      { include: '@tagHandle' },
      { include: '@anchor' },
      { include: '@flowNumber' },

      // Other value (keyword or string)
      [
        /[^\],]+/,
        {
          cases: {
            // '@keywords': 'keyword',
            '@default': 'string',
          },
        },
      ],
    ],

    // First line of a Block Style
    multiString: [[/^( +).+$/, 'string', '@multiStringContinued.$1']],

    // Further lines of a Block Style
    //   Workaround for indentation detection
    multiStringContinued: [
      [
        /^( *).+$/,
        {
          cases: {
            '$1==$S2': 'string',
            '@default': { token: '@rematch', next: '@popall' },
          },
        },
      ],
    ],

    whitespace: [[/[ \t\r\n]+/, 'white']],

    // Only line comments
    comment: [[/#.*$/, 'comment']],

    // Start Flow Collections
    flowCollections: [
      [/\[/, '@brackets', '@array'],
      [/\{/, '@brackets', '@object'],
    ],

    // Start Flow Scalars (quoted strings)
    flowScalars: [
      [/"([^"\\]|\\.)*$/, 'string.invalid'],
      [/'([^'\\]|\\.)*$/, 'string.invalid'],
      [/'[^']*'/, 'string'],
      [/"/, 'string', '@doubleQuotedString'],
    ],

    doubleQuotedString: [
      [/[^\\"]+/, 'string'],
      [/@escapes/, 'string.escape'],
      [/\\./, 'string.escape.invalid'],
      [/"/, 'string', '@pop'],
    ],

    // Start Block Scalar
    blockStyle: [[/[>|][0-9]*[+-]?$/, 'operators', '@multiString']],

    // Numbers in Flow Collections (terminate with ,]})
    flowNumber: [
      [/@numberInteger(?=[ \t]*[,\]}])/, 'number'],
      [/@numberFloat(?=[ \t]*[,\]}])/, 'number.float'],
      [/@numberOctal(?=[ \t]*[,\]}])/, 'number.octal'],
      [/@numberHex(?=[ \t]*[,\]}])/, 'number.hex'],
      [/@numberInfinity(?=[ \t]*[,\]}])/, 'number.infinity'],
      [/@numberNaN(?=[ \t]*[,\]}])/, 'number.nan'],
      [/@numberDate(?=[ \t]*[,\]}])/, 'number.date'],
    ],

    tagHandle: [[/![^ ]*/, 'tag']],

    anchor: [[/[&*][^ ]+/, 'namespace']],
  },
};
